//
//  ChangePsdViewController.m
//  CityFire
//
//  Created by Liuyz on 2017/8/2.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "ChangePsdViewController.h"
#import "UserInfoViewModel.h"
#import "NSString+Tools.h"

@interface ChangePsdViewController ()

@property (nonatomic, weak) IBOutlet UITextField *oldPsdTF;
@property (nonatomic, weak) IBOutlet UITextField *nPsdTF;
@property (nonatomic, weak) IBOutlet UITextField *rnPsdTF;
@property (nonatomic, weak) IBOutlet UIButton    *changeBtn;

@property (nonatomic, strong) UserInfoViewModel *userInfoViewModel;

@end

@implementation ChangePsdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = kChangePsd_TITLE;
    
    [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:kNOTIFICATION_ALERT_PWS object:nil] takeUntil:[self rac_willDeallocSignal]] subscribeNext: ^(NSNotification *notification) {
        [self.navigationController popViewControllerAnimated:YES];
    }];

}

//- (IBAction)<#selector#>:(id)sender


- (IBAction)changePsd:(id)sender
{
    if (!self.oldPsdTF.text.length) {
        [SVProgressHUD showInfoWithStatus:@"请输入旧密码"];
        [self.oldPsdTF becomeFirstResponder];
        
        return;
    }
    if (!self.nPsdTF.text.length) {
        [SVProgressHUD showInfoWithStatus:@"请输入新密码"];
        [self.nPsdTF becomeFirstResponder];
        
        return;
    }
    if (!self.rnPsdTF.text.length) {
        [SVProgressHUD showInfoWithStatus:@"请输入确认新密码"];
        [self.rnPsdTF becomeFirstResponder];
        
        return;
    }
    if (![self.nPsdTF.text isEqualToString:self.rnPsdTF.text]) {
        [SVProgressHUD showInfoWithStatus:@"新密码与确认密码不一致"];
        [self.nPsdTF becomeFirstResponder];
        
        return;
    }
    
    [self.userInfoViewModel changePassword:@{@"newpasswd" : [NSString md5:self.nPsdTF.text] , @"oldpwd" : [NSString md5:self.oldPsdTF.text]}];
}

- (UserInfoViewModel *)userInfoViewModel
{
    if (_userInfoViewModel == nil) {
        _userInfoViewModel = [[UserInfoViewModel alloc] init];
    }
    
    return _userInfoViewModel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
